package cn.iocoder.yudao.module.kt.controller.admin.studyfile;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.kt.controller.admin.studyfile.vo.*;
import cn.iocoder.yudao.module.kt.dal.dataobject.studyfile.StudyFileDO;
import cn.iocoder.yudao.module.kt.service.studyfile.StudyFileService;

@Tag(name = "管理后台 - 学习资料")
@RestController
@RequestMapping("/kt/study-file")
@Validated
public class StudyFileController {

    @Resource
    private StudyFileService studyFileService;

    @PostMapping("/create")
    @Operation(summary = "创建学习资料")
    @PreAuthorize("@ss.hasPermission('kt:study-file:create')")
    public CommonResult<Long> createStudyFile(@Valid @RequestBody StudyFileSaveReqVO createReqVO) {
        return success(studyFileService.createStudyFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新学习资料")
    @PreAuthorize("@ss.hasPermission('kt:study-file:update')")
    public CommonResult<Boolean> updateStudyFile(@Valid @RequestBody StudyFileSaveReqVO updateReqVO) {
        studyFileService.updateStudyFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除学习资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('kt:study-file:delete')")
    public CommonResult<Boolean> deleteStudyFile(@RequestParam("id") Long id) {
        studyFileService.deleteStudyFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得学习资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('kt:study-file:query')")
    public CommonResult<StudyFileRespVO> getStudyFile(@RequestParam("id") Long id) {
        StudyFileDO studyFile = studyFileService.getStudyFile(id);
        return success(BeanUtils.toBean(studyFile, StudyFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得学习资料分页")
    @PreAuthorize("@ss.hasPermission('kt:study-file:query')")
    public CommonResult<PageResult<StudyFileRespVO>> getStudyFilePage(@Valid StudyFilePageReqVO pageReqVO) {
        PageResult<StudyFileDO> pageResult = studyFileService.getStudyFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StudyFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出学习资料 Excel")
    @PreAuthorize("@ss.hasPermission('kt:study-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStudyFileExcel(@Valid StudyFilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StudyFileDO> list = studyFileService.getStudyFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "学习资料.xls", "数据", StudyFileRespVO.class,
                        BeanUtils.toBean(list, StudyFileRespVO.class));
    }

}